<template>
  <div slot="step0" class="importStep">
    <!-- 模板为空 -->
    <div class="templateUpload" v-if="!pdfUrl">
      <div class="icon">
        <i class="el-icon-document elIcon" />
      </div>
      <p>模板空空，赶紧导入...</p>
      <el-upload class="upload-demo" ref="upload" action="/admin/sys-file/upLodeFileAdd" :auto-upload="false" :file-list="fileList" :on-change="handleTemplateChange" accept=".doc, .docx, .pdf">
        <el-button class="exportBtn" type="primary">开始导入</el-button>
        <div slot="tip" class="el-upload__tip">支持导入格式：doc,docx,pdf</div>
      </el-upload>
    </div>
    <!-- 模板不为空 -->
    <div class="templateShow" v-else>
      <div class="fileWrap" @click="previewTemplate">
        <pdf ref="pdf" :src="pdfUrl" :page="1" style="width: 100%" v-if="previewType == 'pdf'" />
        <div class="wordsWrap" v-else>
          <div class="name">
            <p>{{ fileList.length?fileList[0].name:''}}</p>
            <p>模板文件</p>
          </div>
          <div>
            <p>出租方：深圳市福田区物业管理中心</p>
            <p>承租方：{{formData.customerName || '暂无数据'}}</p>
          </div>
          <div class="date">合同签订日期：{{formData.endDate}}</div>
        </div>
      </div>
      <div class="fileWrap">
        <el-upload
          class="upload-demo"
          ref="upload"
          action="/admin/sys-file/upLodeFileAdd"
          :auto-upload="false"
          :file-list="fileList"
          :on-change="handleTemplateChange"
          accept=".doc, .docx, .pdf"
          :show-file-list="false"
        >
          <div class="add">
            <i class="icon el-icon-plus"></i>
            <div class="el-upload__tip">支持导入格式：doc,docx,pdf</div>
          </div>
        </el-upload>
      </div>
      <el-dialog title="合同详情" width="800px" :visible.sync="dialogContractVisible" :close-on-click-modal="false">
        <v-pdf :src="pdfUrl" />
      </el-dialog>
    </div>

    <el-form class="nameForm form-wrap" size="small" :model="nameForm" ref="nameForm">
      <el-form-item label="合同名称:" prop="contractName">
        <el-input v-halfCharCode v-model="nameForm.contractName" placeholder="请输入合同名称" maxlength="20" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import pdf from "vue-pdf";
import vPdf from "@/components/v-pdf";
export default {
  name: 'templateUpload',
  props: {
    // 表单数据
    formData: {
      type: Object,
      default: () => ({})
    }
  },
  components: {
    pdf,
    vPdf
  },
  data () {
    return {
      // 展示的模板附件地址
      pdfUrl: "",
      // 模板文件
      fileList: [],
      // 预览类型
      previewType: '',
      // 模板pdf弹窗展示地址
      dialogContractVisible: false,
      // 合同名称表单
      nameForm: {
        contractName: ""
      },
    }
  },
  methods: {
    // 数据回显初始化
    init (data) {
      this.contractName = data.contractName
      this.pdfUrl = data.pdfUrl
      this.previewType = data.previewType
    },
    // 监听上传模板
    handleTemplateChange (file, fileList) {
      // 如果当前场景是新增
      const previewType = file.name.split(".").pop();
      this.previewType = previewType;
      let url = window.URL.createObjectURL(file.raw);
      this.pdfUrl = url;
      this.fileList = [file];
      this.$alert("模板替换成功，请点击下一步保存新的模板附件");
    },
    // 模板展示点击
    previewTemplate () {
      if (this.previewType == "pdf") {
        this.dialogContractVisible = true;
      } else {
        window.location.href = this.pdfUrl;
      }
    },
  },
}
</script>

<style lang="scss" scoped>
// 导入步骤
.importStep {
  .templateUpload {
    width: 400px;
    margin: 40px auto 0;
    .icon {
      width: 136px;
      height: 136px;
      border-radius: 50%;
      border: 2px solid #e3e3e4;
      display: flex;
      justify-content: center;
      align-items: center;
      margin: 0 auto;
    }
    .elIcon {
      font-size: 70px;
      color: #999;
    }
    p {
      font-size: 14px;
      margin-top: 20px;
      color: #333;
    }
    .exportBtn {
      width: 145px;
      height: 48px;
      font-size: 15px;
      margin-top: 20px;
    }
    .el-upload__tip {
      font-size: 13px;
      color: #999;
    }
  }
  .nameForm {
    display: none;
    width: 600px;
    margin: 40px auto;
  }
  .templateShow {
    padding: 0 20px;
    display: flex;
    justify-content: center;
    .fileWrap {
      width: 400px;
      height: 450px;
      border: 1px solid #eaeaea;
      transition: 0.5s;
      margin-right: 22px;
      overflow: hidden;
      display: flex;
      align-items: center;
      cursor: pointer;
      /deep/ .upload-demo {
        width: 100%;
        height: 100%;
      }
      /deep/ .el-upload {
        width: 100%;
        height: 100%;
      }
      .add {
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        .icon {
          margin: 0 auto;
          font-size: 60px;
          color: #ccc;
        }
      }
      .el-upload__tip {
        color: #999;
        margin-top: 20px;
        font-size: 13px;
      }
    }
  }
  .wordsWrap {
    width: 100%;
    height: 100%;
    padding: 50px 0 50px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    p {
      margin-top: 10px;
    }
  }
}
</style>